package com.storm.utils;

import java.io.File;

/**
 * this class used to manager the folders which we may need while we doing our experiments
 * the way we make our dir is:home dir / user name/ date /STORM/RAWDATA1......
 * 							  home dir / user name/ date /wide field/POS1......
 * @author liuquan
 * @date 2013-12-02
 *
 */
public class FolderManager {
	
	private String homeDir = "D:\\test";//the home dir of the data, we got this from the config
	private String username = "liuquan";//we got this from the config
	private String stormDir = "STORM";
	private String wideFieldDir = "wide field";
	private String curRawDir = "";
	private String curWideFieldDir = "";
	private int count = 1;//the count of the experiment, use this to create the name RAWDATA1.2.3
	private static FolderManager instance = new FolderManager();//
	
	private FolderManager(){}
	
	/**
	 * create the folders
	 */
	public void createFolder(){
		stormDir = homeDir + File.separator+ username + File.separator + UDate.getByDay() + File.separator + stormDir;
		wideFieldDir = homeDir + File.separator+ username + File.separator + UDate.getByDay()+ File.separator + wideFieldDir;
		File storm = new File(stormDir);
		if(!storm.exists())
			storm.mkdirs();
		File widefield = new File(wideFieldDir);
		if(!widefield.exists())
			widefield.mkdirs();
	} 
	
	/**
	 * create a new data folder for new results
	 */
	public void addResultFolder(){
		String rawDir = stormDir + File.separator + "RAWDATA" + count;
		String wideDir = wideFieldDir +File.separator + "POS" + count;
		curRawDir = rawDir;
		curWideFieldDir = wideDir;
		File raw = new File(rawDir);
		File wide = new File(wideDir); 
		if(!raw.exists()){
			raw.mkdirs();
		}
		if(!wide.exists()){
			wide.mkdirs();
		}
		count ++;
	}
	/**
	 * change current user to another user
	 */
	public void changeUser(String username){
		instance.username = username;
		createFolder();//create the folder for the user
		count = 1;
	}
	
	/**
	 * reset the count
	 */
	public void resetCount(){
		instance.count = 1;
		addResultFolder();
	}
	/**
	 * redo the last experiment
	 */
	public void resetLast(){
		instance.count--;
		addResultFolder();
	}
	
	public String getRawDataDir(){
		if(curRawDir == null || curRawDir.equals("")){
			Log.writeErr(UDate.getByMinute() + ": " +instance.username +  ": rawDir is null in FolderManager...");
			addResultFolder();
		}
		return curRawDir;
	}
	public String getWideFieldDir(){
		if(curWideFieldDir == null || curWideFieldDir.equals("")){
			Log.writeErr(UDate.getByMinute() + ": " +instance.username +  ": rawDir is null in FolderManager...");
			addResultFolder();
		}
		return curWideFieldDir;
	}
	
	public static FolderManager getInstance(){
		if(instance == null){
			instance = new FolderManager();
		}
		return instance;
	}
	
	public static void main(String[] args){
		FolderManager.getInstance().createFolder();
		FolderManager.getInstance().addResultFolder();
		System.out.println(FolderManager.getInstance().getRawDataDir());
		System.out.println(FolderManager.getInstance().getWideFieldDir());
		FolderManager.getInstance().addResultFolder();
		System.out.println(FolderManager.getInstance().getRawDataDir());
		System.out.println(FolderManager.getInstance().getWideFieldDir());
	}

}
